﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace WellDone
{
    public partial class Configuration : Page
    {
        public ObservableCollection<ConfigurationRecord> ConfigurationSource = new ObservableCollection<ConfigurationRecord>();
        public ObservableCollection<PaymentTypesRecord> PaymenttypeSource = new ObservableCollection<PaymentTypesRecord>();
        public ServiceReferenceOpenRPOS.Configuration cf;
        public int LastKeyId = -1;
        public byte oldKey;
        public string oldKeyName;
        

        public Configuration()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            init();
        }

        // Executes when the user navigates to this page.
        public void init()
        {
            initConfigurationSetting(false);
            initPaymentTypes(false);
            Globals.showWaitCursor(false);
        }


        public async void initConfigurationSetting(bool showWaitCursor)
        {
            Globals.showWaitCursor(true);
            await Globals.db.GetConfigurationObjects("1=1 order by ID");
            ConfigurationSource.Clear();

            //Fill Data Grid Configuration Item
            foreach (ServiceReferenceOpenRPOS.Configuration config in Globals.db.ConfigurationRecords)
            {
                ConfigurationRecord configurationRecord = new ConfigurationRecord();
                configurationRecord.configDetail = config;
                configurationRecord.KeyName = config.col_KeyName;
                configurationRecord.KeyValue = config.col_Value;
                ConfigurationSource.Add(configurationRecord);
            }

            dgConfigurationItems.ItemsSource = ConfigurationSource;

            Globals.configuration = this;
            Globals.showWaitCursor(false);    
        }

        public async void initPaymentTypes(bool showWaitCursor)
        {
            Globals.showWaitCursor(true);
            await Globals.db.GetPaymentTypeObjects("1=1 order by ID");
            PaymenttypeSource.Clear();

            if (Globals.db.PaymentTypeRecords.Count==0)
            {
                LastKeyId = -1;
            }

            //Fill Data Grid Payment Type
            foreach (ServiceReferenceOpenRPOS.PaymentType paytype in Globals.db.PaymentTypeRecords)
            {
                PaymentTypesRecord paymenttyperecord = new PaymentTypesRecord();
                paymenttyperecord.paymenttype = paytype;
                paymenttyperecord.KeyValue = paytype.col_ID;
                paymenttyperecord.KeyName = paytype.col_Name;
                PaymenttypeSource.Add(paymenttyperecord);

                if (Globals.db.PaymentTypeRecords.IndexOf(paytype) == Globals.db.PaymentTypeRecords.Count - 1)
                {
                    LastKeyId = paytype.col_ID;
                }
            }

            dgPaymentType.ItemsSource = PaymenttypeSource;
            Globals.showWaitCursor(false);

        }

        //Remove Payment Type
        private async void btnRemovePaymentType_Click(object sender, RoutedEventArgs e)
        {
            if (dgPaymentType.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you want to remove the selected payment type?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ServiceReferenceOpenRPOS.PaymentType paymenttype = new ServiceReferenceOpenRPOS.PaymentType();
                    PaymentTypesRecord paymenttypeRecord = (PaymentTypesRecord)(dgPaymentType.SelectedItem);
                    await Globals.db.DeletePaymentTypeObject(paymenttypeRecord.paymenttype);
                    init();
                }
            }
        }

        //Add Configuration
        private void btnAddConfiguration_Click(object sender, RoutedEventArgs e)
        {
            AddConfiguration aconfig = new AddConfiguration();
            aconfig.Show();
        }

        //Add Payment Types
        private void btnAddPaymentType_Click(object sender, RoutedEventArgs e)
        {
            AddPaymentTypeDialog apaymenttype = new AddPaymentTypeDialog();
            if (LastKeyId!=-1)
            {
                apaymenttype.tbPaymentTypeKey.Text = (LastKeyId + 1).ToString();

            }
            else
            {
                apaymenttype.tbPaymentTypeKey.Text = "2";

            }

            apaymenttype.Show();
        }


        //Edit Configuration
        private void btnEditConfiguration_Click(object sender, RoutedEventArgs e)
        {
            if (dgConfigurationItems.SelectedIndex>=0)
            {
                EditConfiguration econfig = new EditConfiguration();
                ConfigurationRecord configurationRecord = (ConfigurationRecord)(dgConfigurationItems.SelectedItem);
                econfig.cf = configurationRecord.configDetail;
                econfig.tbConfigKey.Text = configurationRecord.KeyName;
                econfig.tbConfigValue.Text = configurationRecord.KeyValue;
                econfig.Show();
            }
        }

        // Remove Configuration 
        private async void btnRemoveConfiguration_Click(object sender, RoutedEventArgs e)
         {
            if (dgConfigurationItems.SelectedIndex >= 0)
            {
                if (MessageBox.Show("Are you sure you want to remove the selected configuration?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ServiceReferenceOpenRPOS.Configuration cfg = new ServiceReferenceOpenRPOS.Configuration();
                    ConfigurationRecord configurationRecord = (ConfigurationRecord)(dgConfigurationItems.SelectedItem);
                    await Globals.db.DeleteConfigurationObject(configurationRecord.configDetail);
                    init();
                }   
             }
         }

        //Edit Payment Type
        private void btnEditPaymentType_Click(object sender, RoutedEventArgs e)
        {
            if (dgPaymentType.SelectedIndex>=0)
            {
                EditPaymentTypeDialog epaymenttype = new EditPaymentTypeDialog();
                PaymentTypesRecord paymenttypeRecord = (PaymentTypesRecord)(dgPaymentType.SelectedItem);
                epaymenttype.pt = paymenttypeRecord.paymenttype;
                epaymenttype.tbPaymentTypeKey.Text = paymenttypeRecord.KeyValue.ToString();
                oldKey = paymenttypeRecord.KeyValue;
                epaymenttype.tbPaymentTypeValue.Text = paymenttypeRecord.KeyName;
                oldKeyName = paymenttypeRecord.KeyName;
                epaymenttype.Show();
            }
        }

    }

    public class ConfigurationRecord
    {
        public string KeyName { get; set; }
        public string KeyValue { get; set; }
        public ServiceReferenceOpenRPOS.Configuration configDetail { get; set; }
    }

    public class PaymentTypesRecord
    {
        public string KeyName { get; set; }
        public byte KeyValue { get; set; }
        public ServiceReferenceOpenRPOS.PaymentType paymenttype { get; set; }

    }
}
